<?php

namespace app\common\model;

use basic\ModelBasic;

class TeacherInfo extends ModelBasic
{
	use \traits\ModelTrait;

	/**
	 * 设置where条件
	 * @Author   FW
	 * @DateTime 2021-01-12
	 * @Email    123763712@qq.com
	 * @param    [type]           $where [description]
	 * @param    string           $alias [description]
	 * @param    [type]           $model [description]
	 */
	public static function setWhere($where,$alias='m',$model=null){
        $model = $model===null ? new self() : $model;

        $model->alias($alias);

        if ( isset($where['status']) && $where['status'] != '' ) {
            $model->where(sprintf('%s.status = %d' , $alias , $where['status']));
        }

        if ( isset($where['keyword']) && $where['keyword'] != '' ) {
            $model->where('full_name|mobile','LIKE',"%{$where['keyword']}%");
        }

        if ( static::$softDelete ) {
        	$model->where($alias.'.is_del',0);
        }
        return $model;
    }

    /**
     * 列表
     * @Author   FW
     * @DateTime 2020-09-24
     * @Email    123763712@qq.com
     * @param    [type]
     * @return   [type]
     */
    public static function getList($where){
    	$count = self::setWhere($where)->count();
    	$data  = [];
    	if ( $count == 0 ) {
    		return compact('data','count');
    	}

        $data=self::setWhere($where,'m')
        ->field("m.*,admin.real_name as admin_real_name,u.full_name as user_nickname")
        ->join('__SYSTEM_ADMIN__ admin','m.admin_id = admin.id','left')
        ->join('__USER__ u' , 'm.uid=u.uid','left')
        ->page( $where['page'] ?? 1, $where['limit'] ?? 60)
        ->select();

        if ( !empty($data) ) {
        	$data = $data->toArray();
        	$data = static::process_list($data);
        }

        return compact('data','count');
    }

    /**
     * 处理列表数据
     * @Author   FW
     * @DateTime 2021-01-12
     * @Email    123763712@qq.com
     * @param    array            $list [description]
     * @return   [type]                 [description]
     */
    public static function process_list (array $list)
    {
        return $list;
    }

    /**
     * [getOne description]
     * @Author   FW
     * @DateTime 2021-01-12
     * @Email    123763712@qq.com
     * @param    [type]           $id [description]
     * @return   [type]               [description]
     */
    public static function getOne ($id)
    {
        $item = self::alias('m')->where('m.id',$id)
        	->field("m.*")
            ->find();

        if ( !empty($item) ) {
            $item = $item->toArray();

            $item = static::process_list([$item])[0];
        }
        return $item;
    }
} //Class End
